/**
 * Created by zycool on 2016/11/11.
 * 管道环焊缝风险分析 - 完整性分析
 */


;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点
    var $begin = $('#JQ_begin');
    // 终点
    var $end = $('#JQ_end');
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                if (dataset.bool) {
                    $begin.val(dataset.result.beginMileage);
                    $end.val(dataset.result.endMileage);
                }
            });
        } else {
            $begin.val('');
            $end.val('');
            $segment.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                if (dataset.bool) {
                    $begin.val(dataset.result.beginMileage);
                    $end.val(dataset.result.endMileage);
                }
            });
        } else {
            $begin.val('');
            $end.val('');
        }
    });
    // 导出按钮
    var $export = $('#JQ_export');
    // 查询按钮
    var $search = $('#JQ_search');

    // 查询对象
    var filter = {
        getSearch: function () {
            var data = {};
            data.mode = true;
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                } else {
                    $pipeline.element.TIP({
                        content: '管道名称不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.pipeLineName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                } else {
                    data.mode = false;
                }
                return '';
            }();
            data.startMileage = function () {
                var v = $.trim($begin.val());
                if(v){
                    if(isNaN(v)){
                        $begin.TIP({
                            content: '起点距离须为数字!',
                            zindex: 9
                        });
                        data.mode = false;
                    }else{
                        v = parseFloat(v);
                        if(v >= 0){
                            return v;
                        }else{
                            $begin.TIP({
                                content: '起点距离须大于或等于0!',
                                zindex: 9
                            });
                            data.mode = false;
                        }
                    }
                }else{
                    $begin.TIP({
                        content: '起点距离不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            data.endMileage = function () {
                var v1 = $.trim($begin.val());
                var v2 = $.trim($end.val());
                if (v2) {
                    if (isNaN(v2)) {
                        $end.TIP({
                            content: '终点距离须为数字!',
                            zindex: 9
                        });
                        data.mode = false;
                    } else {
                        v1 = parseFloat(v1);
                        v2 = parseFloat(v2);
                        if(v2 <= v1){
                            $end.TIP({
                                content: '终点距离须大于起点距离!',
                                zindex: 9
                            });
                            data.mode = false;
                        }else{
                            return v2;
                        }
                    }
                } else {
                    $end.TIP({
                        content: '终点距离不能为空!',
                        zindex: 9
                    });
                    data.mode = false;
                }
                return '';
            }();
            return data;
        }
    };

    // 1.环焊缝异常位置分布
    var hww = [
        {MILEAGE:1000},
        {MILEAGE:3000},
        {MILEAGE:5000},
        {MILEAGE:8000},
        {MILEAGE:9000},
        {MILEAGE:11000},
        {MILEAGE:1400},
        {MILEAGE:20000}
    ];

    // 2.返修焊缝位置分布
    var hwwback = [
        {MILEAGE:1000},
        {MILEAGE:3000},
        {MILEAGE:5000},
        {MILEAGE:8000},
        {MILEAGE:9000},
        {MILEAGE:11000},
        {MILEAGE:1400},
        {MILEAGE:20000}
    ];

    // 3.管道高程
    var high = [
        {x: 0, y: 100},
        {x: 100, y: 300},
        {x: 200, y: 200},
        {x: 300, y: 500},
        {x: 400, y: 400},
        {x: 500, y: 600},
        {x: 600, y: 200},
        {x: 700, y: 500}
    ];

    // 4.管道埋深
    var buried = [
        {x: 0, y: 100},
        {x: 100, y: 300},
        {x: 200, y: 200},
        {x: 300, y: 500},
        {x: 400, y: 400},
        {x: 500, y: 600},
        {x: 600, y: 200},
        {x: 700, y: 500}
    ];

    // 5.地质灾害位置分布
    var location = [
        {x: 0, y: 1},
        {x: 1600, y: 1},
        {x: 2000, y: 2},
        {x: 6000, y: 3},
        {x: 10000, y: 1},
        {x: 12000, y: 2},
        {x: 14000, y: 2}
    ];

    // 6.管道基础信息
    var pipeinfo = [
        {x: 0, y: 1, ys: 1},
        {x: 100, y: 1, ys: 2},
        {x: 200, y: 1, ys: 3},
        {x: 300, y: 1, ys: 1},
        {x: 400, y: 1, ys: 3},
        {x: 500, y: 1, ys: 2},
        {x: 600, y: 1, ys: 3},
        {x: 700, y: 1, ys: 1}
    ];

    // 查询
    $search.click(function () {
        var $this = $(this);
        var param = filter.getSearch();
        if (param.mode) {
            $this.attr('data-search','true');
            var mile = DyyBase.getMinMax(param.startMileage, param.endMileage);
            var min = mile.min, max = mile.max;

            // 主数据状态
            var status = false;

            // 1.环焊缝异常位置分布
            DyyBase.ajax(analysisApi.url_insp_hww(), param, function (dataset) {
                if (dataset.bool) {
                    if (dataset.result && dataset.result.length > 0) {

                        status = true;

                        var data = d3Charts.formartYs(dataset.result,{x:'MILEAGE'});
                        d3Charts.signVx({
                            container: 'JQ_hww',
                            color: ['#f09091'],
                            data: data,
                            xAxis: {
                                data: [min, max],
                                ticks: 10,
                                col: 'x'
                            },
                            yAxis: {
                                data: [0, 2],
                                ticks: 2,
                                show: false,
                                col: 'y'
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: ''
                            },
                            pointText: {
                                show: false
                            }
                        });
                        $('#JQ_hww').trigger('playTag','true');
                    }else{
                        $('#JQ_hww').trigger('clear').trigger('playTag','false');
                    }
                }
            },{async:false});

            if(!status){
                $export.attr('data-empty','true');
                $('#JQ_hwwback').trigger('clear').trigger('playTag','false');
                $('#JQ_high').trigger('clear').trigger('playTag','false');
                $('#JQ_buried').trigger('clear').trigger('playTag','false');
                $('#JQ_hazard_location').trigger('clear').trigger('playTag','false');
                $('#JQ_pipeinfo').trigger('clear').trigger('playTag','false');
                $('#JQ_pdf_area').dragLine().clear();
                return false;
            }else{
                $export.attr('data-empty','false');
            }

            // 2.返修焊缝位置分布
            DyyBase.ajax(analysisApi.url_insp_hwwback(), param, function(dataset){
                if (dataset.bool) {
                    //var data = d3Charts.formartYs(hwwback,{x:'MILEAGE'});
                    // dataset.result && dataset.result.length > 0
                    if (dataset.result && dataset.result.length > 0) {
                        var data = d3Charts.formartYs(dataset.result,{x:'MILEAGE'});
                        d3Charts.signVx({
                            container: 'JQ_hwwback',
                            color: ['#f09091'],
                            data: data,
                            xAxis: {
                                data: [min, max],
                                ticks: 10,
                                col: 'x'
                            },
                            yAxis: {
                                data: [0, 2],
                                ticks: 2,
                                show: false,
                                col: 'y'
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: ''
                            },
                            pointText: {
                                show: false
                            }
                        });
                        $('#JQ_hwwback').trigger('playTag','true');
                    }else{
                        $('#JQ_hwwback').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 3.管道高程
            DyyBase.ajax(analysisApi.url_pipe_high(), param, function (dataset) {
                if (dataset.bool) {
                    if (dataset.result && dataset.result.length > 0) {
                        var data = d3Charts.formart(dataset.result, {x: 'x', y: 'y'});
                        d3Charts.dogLeg({
                            container: 'JQ_high',
                            color: ['#49a1e8'],
                            data: data,
                            tip: {
                                xtag: '高程(m)',
                                ytag: '等级'
                            },
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                ticks: 10,
                                render: function(d){
                                    return d.toFixed(1);
                                }
                            },
                            areaShow: true,
                            area: [
                                {
                                    background: '#ff0000',
                                    range: [900]
                                }
                            ],
                            pointText: {
                                startShow: true,
                                stopShow: true
                            },
                            path: {
                                show: true,
                                type: 'area',
                                color: ['#49a1e8', '#49a1e8']
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            }
                        });
                        $('#JQ_high').trigger('playTag','true');
                    } else {
                        $('#JQ_high').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 4.管道埋深
            DyyBase.ajax(analysisApi.url_pipe_buried(), param, function (dataset) {
                if (dataset.bool) {
                    if (dataset.result && dataset.result.length > 0) {
                        var data = d3Charts.formart(dataset.result,{x:'x',y:'y'});
                        d3Charts.dogLeg({
                            container: 'JQ_buried',
                            color: ['#53c36f'],
                            data: data,
                            areaShow: true,
                            area: [
                                {
                                    background: '#ff0000',
                                    range: [1.5]
                                }
                            ],
                            xAxis: {
                                data: [min, max],
                                ticks: 10
                            },
                            yAxis: {
                                data: [],
                                ticks: 6,
                                render: function(d){
                                    return d.toFixed(2);
                                }
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: '',
                                ygrid: true
                            }
                        });
                        $('#JQ_buried').trigger('playTag','true');
                    }else{
                        $('#JQ_buried').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 5.地质灾害位置分布
            DyyBase.ajax(analysisApi.url_geologic_hazard(), param, function (dataset) {
                if (dataset.bool) {
                    // dataset.result && dataset.result.length > 0
                    if (dataset.result && dataset.result.length > 0) {
                        var data = d3Charts.formartYs(dataset.result,{x:'x',y:'y'});
                        d3Charts.signVx({
                            container: 'JQ_hazard_location',
                            color: ['#f09091'],
                            data: data,
                            xAxis: {
                                data: [min, max],
                                ticks: 10,
                                col: 'x'
                            },
                            yAxis: {
                                data: [0, 2],
                                ticks: 2,
                                show: false,
                                col: 'y'
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: ''
                            },
                            signText: function(d){
                                return d.ys;
                            }
                        });
                        $('#JQ_hazard_location').trigger('playTag','true');
                    }else{
                        $('#JQ_hazard_location').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 6.管道基础信息
            DyyBase.ajax(analysisApi.url_pipe_info(), param, function (dataset) {
                if (dataset.bool) {
                    if (dataset.result && dataset.result.length > 0) {
                        var data = d3Charts.formartYs(dataset.result, {x: 'x', y: 'type'});
                        d3Charts.sign({
                            container: 'JQ_pipeinfo',
                            color: ['#7fb9e7', '#7fb9e7', '#7fb9e7'],
                            data: data,
                            xAxis: {
                                data: [min, max],
                                ticks: 10,
                                col: 'x'
                            },
                            yAxis: {
                                data: [0, 2],
                                ticks: 2,
                                show: false,
                                col: 'y'
                            },
                            layout: {
                                margin: {
                                    left: 50,
                                    right: 30,
                                    top: 10,
                                    bottom: 20
                                },
                                xtag: '',
                                ytag: ''
                            },
                            signText: function (d) {
                                switch (d.ys) {
                                    case 'VALVEROOM':
                                        return '阀';
                                        break;
                                    case 'CROSSING_TYPE_CY':
                                        return '穿';
                                        break;
                                    case 'CROSSING_TYPE_KY':
                                        return '跨';
                                        break;
                                    default:
                                        return '无';
                                        break;
                                }
                            }
                        });
                        $('#JQ_pipeinfo').trigger('playTag','true');
                    } else {
                        $('#JQ_pipeinfo').trigger('clear').trigger('playTag','false');
                    }
                }
            });

            // 启用拖拽
            $('#JQ_pdf_area').dragLine();
        }
    });

    // 导出
    $export.click(function () {
        var $this = $(this);
        if($this.attr('data-empty') == 'true'){
            $this.MSG({
                content: '没有相关数据,将不能进行导出!'
            });
            return false;
        }
        DyyBase.isLogin(function(){
            var param = filter.getSearch();
            var search = $search.attr('data-search');
            if(param.mode){
                if(search === 'true'){
                    $this.EXPT({
                        drag: true,
                        type: 'name',
                        name: param.pipeLineName + '管道环焊缝风险分析图',
                        yesBtnAct: function(callback,fname){
                            callback();
                            canvasPdf.exportHtmlPdf({
                                dom: $('#JQ_pdf_area'),
                                server: DyyBase.root_path + '/comm/downImgPdf.json',
                                param: {
                                    fileName: fname,
                                    name: fname
                                }
                            });
                        }
                    });
                }else{
                    $this.MSG({
                        content: '尚未绘制图形,请先执行查询!'
                    });
                }
            }
        });
    });


})(jQuery, window, document);